package java8.lambda.demo01;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ChangLiang
 * @date 2020/4/22
 */
public class AppleFilter {

    /**
     * 过滤颜色这个维度
     * @param apples
     * @param color
     * @return
     */
    public static List<Apple> findApple(List<Apple> apples, String color) {
        List<Apple> list = new ArrayList<>();
        for (Apple apple : apples) {
            if (color.equals(apple.getColor())) {
                list.add(apple);
            }
        }
        return list;
    }

    /**
     * 过滤重量这个维度
     * @param apples
     * @param weight
     * @return
     */
    public static List<Apple> findApple(List<Apple> apples, int weight) {
        List<Apple> list = new ArrayList<>();
        for (Apple apple : apples) {
            if (apple.getWeight()>weight) {
                list.add(apple);
            }
        }
        return list;
    }

    /**
     * 仍旧使用一个方法，添加一个标志位，来区分是color还是weight
     * @param apples
     * @param filter
     * @param isColor
     * @param <T>
     * @return
     */
    public static <T> List<Apple> findApple(List<Apple> apples, T filter, boolean isColor) {
        List<Apple> list = new ArrayList<>();
        for (Apple apple : apples) {
            if (isColor) {
                String color = (String) filter;
                if (color.equals(apple.getColor())) {
                    list.add(apple);
                }
            }else{
                Integer weight = (Integer) filter;
                if (apple.getWeight()>weight) {
                    list.add(apple);
                }
            }
        }
        return list;
    }


}
